<template>
  <MesBox title="领料" v-model="visible" :width="1200" :height="600">
    <MesTable
      :columns="columns"
      :tableData="tableData"
      :clickEdit="true"
      :loadKey="true"
      :ck="false"
    ></MesTable>
    <template #footer>
      <!-- <el-button type="primary" @click="print">
        <i class="el-icon-printer"></i>
        打印</el-button
      > -->
      <el-button type="danger" @click="save">
        <i class="el-icon-check"></i>
        保存</el-button
      >
    </template>
  </MesBox>
</template>

<script setup>
import MesBox from '@/components/basic/MesBox.vue'
import MesTable from '@/components/basic/MesTable.vue'
import { getCurrentInstance, ref } from 'vue'

defineEmits(['parentCall'])
const that = getCurrentInstance().appContext.config.globalProperties
let prints =  ref(null)
let visible = ref(false)
let tableData = ref([])
let search = null
let columns = [
  {
    field: 'ProductName',
    title: '产品名称',
  },
  {
    field: 'ProductCode',
    title: '产品编号',
  },
  {
    field: 'Specification',
    title: '规格',
  },
  {
    field: 'Qnty',
    title: '领料数',
    required: true,
    edit: {
      type: 'number',
      keep: true,
    },
  },
  {
    field: 'MaterialTakerMan',
    title: '领料人',
    required: true,
    edit: {
      type: 'select',
      keep: true,
    },
    bind: {
      key: 'user',
      data: [],
    },
  },
]

const open = (selectData, searchFun, printFun) => {
  tableData.value = selectData
  search = searchFun
  prints.value = printFun
  visible.value = true
}

const save = async () => {
  let filterTable = tableData.value.find(
    (item) => !item.Qnty || !item.MaterialTakerMan
  )
  if (filterTable !== undefined) return that.$message.error('请填写完整信息')
  let param = {
    addOutRawMaterial: tableData.value,
  }
  let res = await that.http.post(
    'api/Ware_OutRawMaterial/InOutRawMaterialUpdate',
    param,
    true
  )
  if (res.status) {
    that.$message.success(res.message)
    tableData.value = []
    visible.value = false
    search()
  } else {
    that.$message.error(res.message)
  }
}

// const print = () => {
//   let filterTable = tableData.value.find(
//     (item) => !item.Qnty || !item.MaterialTakerMan
//   )
//   if (filterTable !== undefined) return that.$message.error('请填写完整信息')
//   prints.value([[1,2,3]])
// }

defineExpose({ open })
</script>

<style lang="less" scoped></style>
